<template>
  <div class="zong">
    <div class="top">
      <el-button type="primary" icon="el-icon-plus" @click="add">新建</el-button>
      <el-button type="primary" icon="el-icon-minus" :disabled="disabled" @click="checkDel">删除</el-button>
      <el-button type="primary" icon="el-icon-refresh-right" @click="shuaxin">刷新</el-button>
      <el-button type="primary" style="float: right" @click="daoruDialog = true">导入物料类型</el-button>
    </div>
    <div class="bottom">
      <el-table
        :data="list"
        style="width: 100%; margin-bottom: 20px"
        row-key="Id"
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="checked" type="selection" align="center" width="50" />
        <el-table-column prop="name" label="物料分类名称" width="350">
          <template v-slot="{ row }">
            {{ row.title }}
          </template>
        </el-table-column>
        <el-table-column prop="code" label="物料分类编码" width="350">
          <template v-slot="{ row }">
            {{ row.Code }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="180">
          <template v-slot="{ row }">
            {{ row.Remark }}
          </template>
        </el-table-column>
        <el-table-column prop="action" label="操作" width="307">
          <template slot-scope="scope">
            <div class="action">
              <span @click="editt(scope.row)">编辑</span>
              <span style="margin-left: 5px; margin-right: 5px; color: #ececec">|</span>
              <span @click="del(scope.row)">删除</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pages" :data="list">
      <template>
        <span>总数：{{ list.total }}</span>
        <span>当前：</span>
      </template>
    </div>
    <!-- 新增弹框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="40%">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="上级物料分类:" :label-width="formLabelWidth">
          <el-select ref="selectTree" v-model="form.Text" placeholder="请选择上级物料分类" style="width: 445px">
            <el-option value="data" class="options">
              <el-tree :data="data" :props="defaultProps" @node-click="nodeClick" />
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料分类编码:" :label-width="formLabelWidth">
          <el-input
            autocomplete="off"
            :disabled="(edit = true)"
            placeholder="系统自动生成"
            prefix-icon="el-icon-full-screen"
          />
        </el-form-item>
        <el-form-item label="物料分类名称:" :label-width="formLabelWidth" prop="Name">
          <el-input v-model="form.Name" autocomplete="off" prefix-icon="el-icon-paperclip" />
        </el-form-item>
        <el-form-item label="备注:" :label-width="formLabelWidth">
          <el-input v-model="form.Remarks" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="NO">取 消</el-button>
        <el-button type="primary" @click="OK">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 导入物料类型 -->
    <el-dialog title="导入物料类型" :visible.sync="daoruDialog" width="25%">
      <el-button icon="el-icon-upload2" type="primary" style="width: 35%;" @click="upLoad"><input
        ref="excel-upload-input"
        type="file"
        accept=".xlsx,.xls"
        @change="shangChuan"
      ></el-button>
      <el-button
        icon="el-icon-download"
        style="color: rgb(64,158,255);width: 35%;float: right;"
        @click="downLoad"
      >下载模板表</el-button>
    </el-dialog>
  </div>
</template>
<script>
import { addWuliaoLX, delWuliaoLX, upLoadWL, downLoadWL, editWuliaoLX, getTree, bianji } from '@/api/wuliaoleixing'
import FileSaver from 'file-saver'

export default {
  data() {
    return {
      dialogFormVisible: false,
      daoruDialog: false,
      form: {
        Name: '',
        Remarks: '',
        ParentId: '',
        Text: ''
      },
      editForm: {
        Name: '',
        Remarks: '',
        ParentId: '',
        Code: '',
        Id: ''
        // IsLeaf:'',
        // Path:'',
        // CreateTime:'',
        // CreatorId:'',
        // Deleted:'',
      },
      formLabelWidth: '120px',
      rules: { Name: [{ required: true, message: '请输入物料分类名称', trigger: 'blur' }] },
      disabled: false,
      checked: false,
      list: [],
      hasChildren: true,
      title: '',
      data: [{
        Text: '',
        children: [{
          Text: '',
          children: [{
            Text: ''
          }]
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'Text'
      },
      multipleSelection: [] // 多选的数据
    }
  },
  created() {
    this.getList()
  },
  methods: {
    rowClass() {
      return {
        fontWeight: 'normal',
        color: 'black',
        borderRadius: '5px,5px,5px,5px'
      }
    },
    // 获取列表
    async getList() {
      const res = await getTree()
      console.log(res)
      this.list = res
      this.data = res
    },
    // 添加/编辑
    OK() {
      this.$refs.form.validate(async() => {
        // 添加
        if (this.title === '新增物料类型') {
          if (this.form.Name === '') {
            this.$message.error('请输入物料分类名称')
            return
          }
          await addWuliaoLX(this.form)
          this.$message.success('新增角色成功')
          this.getList()
          this.NO()
          // 编辑
        } else if (this.title === '编辑物料类型') {
          await editWuliaoLX(this.form)
          this.$message.success('修改成功')
          this.getList()
          this.NO()
        }
      })
    },
    // 取消
    NO() {
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(async() => {
          console.log(Object.values(row))
          await delWuliaoLX(Object.values(row))
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 刷新
    shuaxin() {
      this.getList()
    },
    // 多选
    isCheck() {
      console.log(1)
      if (this.checked === true) {
        this.disabled = false
      }
    },
    // 批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log('multipleSelection', this.multipleSelection)
    },
    checkDel() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(async() => {
          console.log(this.multipleSelection)
          await delWuliaoLX(this.multipleSelection.map(item => item.Id))
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改
    async editt(row) {
      this.dialogFormVisible = true
      this.title = '编辑物料类型'
      console.log('list', this.list)
      console.log('row', row.Id)
      // console.log('editForm',this.editForm);
      const res = await bianji({ id: row.Id })
      console.log(res)
      this.form = res
    },
    // 上传数据
    async shangChuan(e) {
      const files = e.target.files
      if (files.length > 0) {
        const data = new FormData()
        files[0]
        data.append('file', files[0])
        await upLoadWL(data)
      }
    },
    // 上传数据
    upLoad() {
      this.$refs['excel-upload-input'].click()
    },
    // 下载模板表
    async downLoad() {
      const res = await downLoadWL()
      FileSaver.saveAs(res, '模板.xlsx')
    },
    // 添加
    async add() {
      this.dialogFormVisible = true
      this.title = '新增物料类型'
      const res = await getTree()
      console.log(res)
      this.list = res
    },
    // 下拉树点击
    nodeClick(data) {
      console.log(data)
      this.form.Text = data.Text
      this.form.ParentId = data.Id
      this.$refs.selectTree.visible = false
      console.log(this.form)
    }
  }
}
</script>
<style scoped lang="scss">
.zong {
  margin: 20px;
  padding: 20px;
  padding-bottom: 50px;
}

.bottom {
  margin-top: 20px;
  margin-bottom: 20px;
}

::v-deep .el-table__body tr:hover>td {
  background-color: rgb(230, 247, 255) !important;
}

.pages {
  float: right;
  font-size: 14px;
}

.action span {
  color: rgb(24, 144, 255);
}

.options {
  height: auto;
}
</style>
